package chat;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

/**
 * @author 张开顺
 * @title: MyWebSocketChannelHandler
 * @projectName netty
 * @description: TODO
 * @date 2019/4/1213:20
 */
public class MyWebSocketChannelHandlerInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        //HttpServerCodec: 针对http协议进行编解码
        channel.pipeline().addLast("http-codec",new HttpServerCodec());
        channel.pipeline().addLast("http-chunket",new ChunkedWriteHandler()); //向客户端发送HTML5文件
        /**
         * 作用是将一个Http的消息组装成一个完成的HttpRequest或者HttpResponse，那么具体的是什么
         * 取决于是请求还是响应, 该Handler必须放在HttpServerCodec后的后面
         */
        channel.pipeline().addLast("aggregator",new HttpObjectAggregator(65536));
        //用于处理websocket, /ws为访问websocket时的uri
        channel.pipeline().addLast("webSocketProtocoHandler",new WebSocketServerProtocolHandler("/websocket",null,true));
       // channel.pipeline().addLast("handler",new MyWebSocketHandler());
        channel.pipeline().addLast("handler",new MyWebSocketHandlerTwo());

    }
}
